﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using Microsoft.VisualStudio.TextTemplating;
using DatabaseToCode.ExtensionMethods;
using Microsoft.Data.ConnectionUI;
using TemplatePackage.MvcTemplate;

namespace DatabaseToCode
{
    public partial class ProjectInformationView : Form
    {
        private InputInformations inputInformations = new InputInformations();

        private EnvDTE.DTE dte;

        /// <summary>
        /// Initializes a new instance of the <see cref="Form1"/> class.
        /// </summary>
        public ProjectInformationView() : this(new InputInformations(), null)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectInformationView"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        public ProjectInformationView(InputInformations viewModel, EnvDTE.DTE dte)
        {
            InitializeComponent();

            this.dte = dte;

            this.inputInformations = viewModel;
            this.connectionStringTextbox.DataBindings.Add("Text", this.inputInformations, "ConnectionString");
            this.providerTextbox.DataBindings.Add("Text", this.inputInformations, "ProviderName");
            this.schemaTextbox.DataBindings.Add("Text", this.inputInformations, "ContextName");
            this.namespaceTextbox.DataBindings.Add("Text", this.inputInformations, "Namespace");
            this.outputTextbox.DataBindings.Add("Text", this.inputInformations, "OutputDirectory");
        }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void generateButton_Click(object sender, EventArgs e)
        {
            //var databaseAnalyser = new DatabaseAnalyser(this.inputInformations.ProviderName, this.inputInformations.ConnectionString);
            //var schema = databaseAnalyser.Analyse();
            //schema.Name = this.inputInformations.ContextName; ;

            //foreach (var table in schema.Tables)
            //{
            //    ModelTemplate mt = new ModelTemplate();
            //    mt.Session = new Dictionary<string, object>();
            //    mt.Session.Add("Table", table.Value);
            //    mt.Session.Add("Namespace", this.inputInformations.Namespace);
            //    mt.Initialize();
            //    var result = mt.TransformText();

            //    File.WriteAllText(this.inputInformations.OutputDirectory + "\\" + table.Value.ClassName.Singuliarize() + "Model.cs", result);

            //    ControllerTemplate ct = new ControllerTemplate();
            //    ct.Session = new Dictionary<string, object>();
            //    ct.Session.Add("Table", table.Value);
            //    ct.Session.Add("Schema", schema);
            //    ct.Session.Add("Context", this.inputInformations.ContextName);
            //    ct.Session.Add("Namespace", this.inputInformations.Namespace);
            //    ct.Initialize();
            //    result = ct.TransformText();

            //    File.WriteAllText(this.inputInformations.OutputDirectory + "\\" + table.Value.ClassName.Singuliarize() + "Controller.cs", result);

            //}

            //DataContextTemplate dct = new DataContextTemplate();
            //dct.Session = new Dictionary<string, object>();
            //dct.Session.Add("Schema", schema);
            //dct.Session.Add("Namespace", this.inputInformations.Namespace);
            //dct.Session.Add("Context", this.inputInformations.ContextName);
            //dct.Initialize();
            //var result2 = dct.TransformText();

            //File.WriteAllText(this.inputInformations.OutputDirectory + "\\" + this.inputInformations.ContextName + ".cs", result2);

            //MessageBox.Show("Done !", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);


            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the chooseDataBaseButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void chooseDataBaseButton_Click(object sender, EventArgs e)
        {
            Microsoft.Data.ConnectionUI.DataConnectionDialog dialog = new DataConnectionDialog();
            DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
            dcs.LoadConfiguration(dialog);

            if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK) {
                this.inputInformations.ConnectionString = dialog.ConnectionString;
                this.inputInformations.ProviderName = dialog.SelectedDataProvider.Name;

                dcs.SaveConfiguration(dialog);
            }
        }

        /// <summary>
        /// Handles the Click event of the browseButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void browseButton_Click(object sender, EventArgs e)
        {
            using (var folderDialog = new FolderBrowserDialog()) {
                folderDialog.RootFolder = Environment.SpecialFolder.Desktop;
                folderDialog.ShowNewFolderButton = true;

                if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                    this.inputInformations.OutputDirectory = folderDialog.SelectedPath;
                }
            }
        }

    }
}
